<template>
  <div class="pay-channels-wrapper">
    <div v-if="channels.length" class="channel-list" :class="{'show': isShow}">
      <div v-for="(channel, idx) in channels" :key="idx" class="channel" :class="channel.id" @click="selectChannel(channel)" v-if="channel.enable">
        <div class="icon-wrapper">
          <i class="icon" :class="`fdx-icons-${channel.id}`"></i>
        </div>
        <div class="content">
          <div class="title">{{channel.description}}支付</div>
          <div class="memo">{{channel.memo}}</div>
        </div>
        <div class="operation">
          <i class="icon fdx-icons-ok" v-show="value.id===channel.id"></i>
          <div class="icon unselected" v-show="value.id!==channel.id"></div>
        </div>
      </div>
    </div>
    <div v-else class="no-pay-channel" :class="{'show': isShow}">
      手机未安装任何支付通道！
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Object,
      default: () => {
        return {
          id: null
        }
      }
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      channels: [],
      channelInfos: {
        wxpay: {
          memo: '亿万用户的选择，更快更安全',
          enable: true
        },
        alipay: {
          memo: '数亿用户都在用，安全可托付',
          enable: false
        }
      },
      isShow: false
    }
  },
  created() {
    this.getChannels()
  },
  mounted() {
  },
  methods: {
    getChannels() {
      if (plus.payment) {
        Indicator.open()
        plus.payment.getChannels((channels) => {
          channels.forEach(item => {
            Object.assign(item, this.channelInfos[item.id])
            if (this.value.id && this.value.id === item.id) {
              this.selectChannel(item)
            }
          })
          this.channels = channels
          this.isShow = true
          Indicator.close()
        }, (e) => {
          Indicator.close()
          Toast('获取支付通道失败！')
        })
      } else {
        this.channels = [
          {
            id: 'wxpay',
            description: '微信'
          },
          {
            id: 'alipay',
            description: '支付宝'
          }
        ]
        this.channels.forEach(item => {
          Object.assign(item, this.channelInfos[item.id])
        })
        this.isShow = true
      }
      return this.channels
    },
    selectChannel(channel) {
      this.$emit('input', channel)
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../assets/sass/_vars';

.pay-channels-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: column;
  align-items: center;

  .channel-list {
    display: none;
    width: 100%;

    &.show {
      display: flex;
      flex-flow: column;
    }

    .channel {
      display: flex;
      width: 100%;
      max-width: 100%;
      height: 60px;
      padding: 0px 10px 0px 0px;
      align-items: center;

      &.wxpay {
        .icon-wrapper {
          .icon {
            &::before {
              color: #00C800;
            }
          }
        }
      }

      &.alipay {
        .icon-wrapper {
          .icon {
            &::before {
              color: #00A0E8;
            }
          }
        }
      }

      .icon-wrapper {
        height: 50px;
        display: flex;
        justify-content: flex-start;
        align-items: center;

        .icon {
          display: flex;
          flex-grow: 1;
          height: 100%;
          align-items: center;
          justify-content: center;

          &::before {
            font-size: 36px;
          }
        }
      }

      .content {
        flex-grow: 1;
        padding-left: 5px;

        .title {
          font-size: 16px;
          line-height: 20px;
        }

        .memo {
          font-size: 12px;
          color: $text-gray;
        }
      }

      .operation {
        width: 40px;
        height: 40px;
        display: flex;
        justify-content: center;
        align-items: center;

        .icon {
          &::before {
            font-size: 32px;
            color: #FF9061;
          }
        }

        .unselected {
          width: 26px;
          height: 26px;
          border-radius: 50%;
          border: solid 1px #aaa;
        }
      }
    }
  }

  .no-pay-channel {
    display: none;
    justify-content: center;
    align-items: center;
    height: 50px;
    font-size: 18px;
    background-color: white;

    &.show {
      display: flex;
    }
  }
}
</style>
